/**
 * 活动管理管理页面js
 * 版权所有：上海戈品投资管理有限公司
 *   author king
     功能说明：活动管理
     创建日期：2015-10-19
     修改记录：
     备注说明：
 */

$(document).ready(function() {
	activeList();
});


/**
 * 查询代理人列表
 * @returns
 */
var activeList = function(){
	 var reissueQuery=$("#selectList").val();
	 var submitTime = $("#submitTime").val();//提交时间
	 var sendTime = $("#sendTime").val();//发快递时间
	 var courNickName = $("#courNickName").val();//用户昵称
	 var courNumber = $("#courNumber").val();//快递单号
	 var courName = $("#courName").val();//快递员名称
	 var courTel = $("#courTel").val();//快递员手机号
	 var activeType=$("#activeType").val();
	 var sendRDStatus=$("#sendRDStatus").val();//发放奖励状态
	 
	 
	var datas =  {"p":"redPackActiveManage","m":"list","reissueQuery":reissueQuery,"submitTime":submitTime,"sendTime":sendTime,"courNickName":courNickName,"courNumber":courNumber,"courName":courName,"courTel":courTel,"activeType":activeType,"sendRDStatus":sendRDStatus};
	var columnDefine = {"id":"编号","senderUserID":"发件人ID","senderOpenID":"发件人openId","senderNickName":"发件人昵称","sendTime":"发件时间","submitTime":"提交时间","expressCode":"快递单号","bCode":"业务码","courierUserID":"快递员Id","courierOpenID":"快递员openId","courierName":"快递员姓名","courierPhone":"快递员手机","forSenderMoney":"发件人红包","forCourierMoney":"快递员红包","redPackStatus":"红包发放状态","status":"审核状态"};
	
	var formatters={
	            "submitTime": function (column, rowData)
	            {
       				var newTime= new Date(rowData.submitTime*1000);
       				var rowText=newTime.getFullYear()+'-'+getTheSureTime((newTime.getMonth()+1))+'-'
       				+getTheSureTime(newTime.getDate())+' '+getTheSureTime(newTime.getHours())+':'
       				+getTheSureTime(newTime.getMinutes())+':'+getTheSureTime(newTime.getSeconds());
       				return rowText;
	            },
	            "expressCode": function (column, rowData)
	            {
	            	if(rowData.type==1){
	   					return "<a href='http://m.kuaidi100.com/index_all.html?postid="+rowData.expressCode+"' " +"target='_blank'>"+rowData.expressCode+"</a>";
	   				}
	   				return "";
	            },
	            "status": function (column, rowData)
	            {
	   				if(rowData.status=="1"){
	   					return "已通过审核";
	   				}else if(rowData.status=="2"){
	   					return "未通过审核";
	   				}else{
	   					return "未审核";
	   				}
	            },
	            "redPackStatus": function (column, rowData)
	            {
	   				if(rowData.redPackStatus=="3"){
	   					return "已发放";
	   				}else if(rowData.redPackStatus=="2"){
	   					return '<a title="用户:'+rowData.forSenderMoney+'" onclick="interfaceInfo(\''+rowData.senderUserID+'\')">用户未发放</a>';
	   				}else if(rowData.redPackStatus=="1"){
	   					return '<a title="快递员:'+rowData.forCourierMoney+'" onclick="interfaceInfo(\''+rowData.courierUserID+'\')">快递员未发放</a>';
	   				}else if(rowData.redPackStatus=="0"){
	   					return '<a title="快递员:'+rowData.forCourierMoney+'&#13;用户:'+rowData.forSenderMoney+'" onclick="interfaceInfo(\''+rowData.courierUserID+'\')">均未发放</a>';
	   				}
	            },
	            "todayRedForCourider": function (column, rowData)
	            {
   					var todayRedForCourider=rowData.todayRedForCourider;
       				if(todayRedForCourider=="2"){
       					return "补发待审核";
       				}else if(todayRedForCourider=="3"){
       					return "审核不通过";
       				}else if(todayRedForCourider=="4"){
       					return "已补发成功";
       				}else if(todayRedForCourider=="5" || todayRedForCourider=="1"){
       					return "直接发放";
       				}else if(todayRedForCourider=="6"){
       					return "无结果";
       				}else if(todayRedForCourider=="7"){
       					return "审核通过";
       				}else if(todayRedForCourider=="8"){
       					return "待定";
       				}else if(todayRedForCourider=="0"){
       					return "";
       				}else{
       					return "";
       				}
	            },
	            "rollStatus": function (column, rowData)
	            {
	   				if(rowData.redPackStatus=="3"){
	   					return "";
	   				}else if(rowData.redPackStatus=="2"){
	   					if(rowData.RedForUser==0 && rowData.ext1=='取消可疑'){
		   					return '<a onclick="redpackagePrompt(this,\''+rowData.forSenderMoney+'\',\'0\',\'1\',2)">补发</a>';
		   					'<div style="display:none">'+JSON.stringify(rowData)+'</div>';
	   					}else{
	   						return "";
	   					}
	   				}else if(rowData.redPackStatus=="1"){
	   					return '';
	   				}else if(rowData.redPackStatus=="0"){
	   					if(rowData.RedForUser==0 && rowData.ext1=='取消可疑'){
	   						//var redpackagePrompt=function(this,rowData.forSenderMoney,0,1,2){
		   					return '<a onclick="redpackagePrompt(this,\''+rowData.forSenderMoney+'\',\'0\',\'1\',2)">补发</a>';
		   					'<div style="display:none">'+JSON.stringify(rowData)+'</div>';
	   					}else{
	   						return "";
	   					}
	   				}
	            },
	            "ext1": function (column, rowData)
	            {
	   				if(rowData.RedForUser==1)
	   				{
	   					return '<span> 可疑用户</span>';
	   				}
	   				else
	   				{
	   	   				return '<span> '+rowData.ext1+'</span>';
	   				}
	            }
	 };
	 var buttons =''; //操作按钮
		buttons+='<p>';
		//buttons+='<button type=\'button\' class=\'btn btn-w-m btn-info\' onclick="statusReset();">设置/取消可疑</button>&nbsp;&nbsp;';
		buttons+='<button type=\'button\' class=\'btn btn-w-m btn-danger\' onclick="rAuditOperate(3);">审核不通过</button>&nbsp;&nbsp;';
		buttons+='<button type=\'button\' class=\'btn btn-w-m btn-primary\' onclick="rAuditOperate(7);">审核通过</button>&nbsp;&nbsp;';
		buttons+='<button type=\'button\' class=\'btn btn-w-m btn-warning\' onclick="rAuditOperate(8);">审核待定</button>&nbsp;&nbsp;';
		buttons+='</p>';
	   hui.pager('service',datas ,"dataList", columnDefine , formatters , buttons ); //分页查询
	
};

/**
 * 查看红包发放记录
 */
var interfaceInfo = function(openid){
	window.location.href='page?p=page/redPackInterfaceManage&userId='+openid+'&flag=0';
};

/**
 * 打开窗口
 * @param divId
 * @returns
 */
var showWin = function( divId ){
	$('#setupWin').modal('hide');
	$('#'+divId+' input').val('');
	$('#'+divId+'').modal('show');
};

/**
 * 打开设置窗口
 * @returns
 */
var showSetupWin = function(){
	
	var json = common.ajaxRequest('service', {"p":"redPackActiveManage","m":"queryActiveState"} , 'POST');
	if( json && json.result == 0 )
	{
		var state = json.state;
		if( state == 0 )
		{
			$('#setupWin .openBtn').addClass('btn-primary');
			$('#setupWin .closeBtn').addClass('btn-white');
		}	
		else
		{
			$('#setupWin .closeBtn').addClass('btn-primary');
			$('#setupWin .openBtn').addClass('btn-white');
		}	
	}
	$('#setupWin').modal('show');
};

/**
 * 审核操作
 * @returns
 */
var rAuditOperate = function( status ){
	
	var data = $('#pop_win .data').text();
	var json = eval('('+data+')');
	var datas={"p":"redPackActiveManage","m":"reissueOperate","id":json.id,"reissueAuditType":status};
	var rjson=common.ajaxRequest('service', datas, "POST");
	if(rjson.result=="0" )
	{
		activeList();
		$('#pop_win').modal('hide');
		common.showMsg('info', '', '设置成功!');
	}
	else
	{
		common.showMsg('error', '', '设置失败!');
		return;
	}
};

/**
 * 查看活动用户/特权用户
 * @returns
 */
var privilegUserManage = function(){
	window.location.href='page?p=page/privilegUserManage';
};

/**
 * 已发放红包记录
 * @returns
 */
var showActiveLogs = function(){
	window.location.href='page?p=page/redPackInterfaceManage&flag=0';
};

/**
 * 红包开启/关闭
 * @returns
 */
var modifyActiveStatus = function( status ){
	
	var json = common.ajaxRequest('service', {"p":"redPackActiveManage","m":"modifyActiveState","status":status} , 'POST');
	if( json && json.result == 0 )
	{
		if( status == 0 )
		{
			$('#setupWin .openBtn').removeClass('btn-primary');
			$('#setupWin .openBtn').removeClass('btn-white');
			$('#setupWin .closeBtn').removeClass('btn-primary');
			$('#setupWin .closeBtn').removeClass('btn-white');
			
			$('#setupWin .openBtn').addClass('btn-primary');
			$('#setupWin .closeBtn').addClass('btn-white');
		}	
		else
		{
			$('#setupWin .openBtn').removeClass('btn-primary');
			$('#setupWin .openBtn').removeClass('btn-white');
			$('#setupWin .closeBtn').removeClass('btn-primary');
			$('#setupWin .closeBtn').removeClass('btn-white');
			
			$('#setupWin .closeBtn').addClass('btn-primary');
			$('#setupWin .openBtn').addClass('btn-white');
		}	
		common.showMsg('info', '' , '设置成功!');
	}
	else
	{
		common.showMsg('error', '' , '设置失败!');
	}	
};


/**
 * 导出数据
 * @returns
 */
var exportData = function( type ){
	
	if( type == 1 ) //按时间导出数据
	{
		var date = $('#exportDataWin .sendTime').val();
		if(date==""||date==null){
			common.showMsg('error', '' , '请填写提交日期！');
			return;
		}
		var sql = "select id,FROM_UNIXTIME(submittime),senderNickName,FROM_UNIXTIME(sendTime),expressCode,courierName,courierPhone,"+
		"case status when 1 then '已通过审核' when 2 then '未通过审核' else '未审核' end ,"+
		"case redPackStatus when 3 then '已发放' else '未发放' end ,"+
		"case todayRedForCourider when 1 then '直接发放' when 2 then '补发待审核' when 3 then '审核不通过' when 4 then '已补发成功' when 5 then '直接发放' when 6 then '无结果' when 7 then '审核通过' when 8 then '待定' else '' end "
		+"from v_redPack_activeList where 1=1 and date(FROM_UNIXTIME(submittime))='"+date+"' order by id asc";
		var data = {"p":"exportData","m":"exportData","sql":sql};
		var json = common.ajaxRequest('service', data , 'POST');
		if( json && json.result == 0 )
		{
			$('#exportDataWin').modal('hide');
			window.open(json.excelFile);
		}
		else
		{
			common.showMsg('error', '' , '导出异常！');
			return;
		}	
		
	}
	if( type == 2 )
	{
		var size = courierNumberSize;
		var date = $('#exportDataWin .sendTime').val();
		if(date==""||date==null){
			common.showMsg('error', '' , '请填写提交日期！');
			return;
		}
		var sql = "select m.ID,m.real_name as 快递员,m.telephone as 手机号,m.company as 公司,m.expressCode as 快递单号,n.location_name,m.senderNickName as 对应用户,m.submittime as 发快递时间,m.courierTime as 录入时间 from "
			+"(select a.ID,a.real_name,a.telephone,a.company,b.expressCode,b.senderOpenID,b.senderNickName,FROM_UNIXTIME(b.submitTime) as submittime,b.courierTime from 用户 a,redpackactivity b where a.openid in ("
			+"select courierOpenid from (select courierOpenID,count(courierOpenID) as count from redpackactivity where date(FROM_UNIXTIME(submitTime))='"+date+"' and todayRedForCourider=3  GROUP BY courierOpenID) as b where b.count>="+size+" ) and a.openID=b.courierOpenID and date(FROM_UNIXTIME(submitTime))='"+date		
			+"' and b.todayRedForCourider=3 ORDER BY a.id) as m,用户 n where m.senderOpenid=n.openid;";
		
		var data = {"p":"exportData","m":"exportData","sql":sql};
		var json = common.ajaxRequest('service', data , 'POST');
		if( json && json.result == 0 )
		{
			window.open(json.excelFile);
		}
		else
		{
			common.showMsg('error', '' , '导出异常！');
			return;
		}	
	}	
	
};

/**
 *快递单自动审核操作
 */
var kddAudit=function(){
	var datas={"p":"redPackActiveManage","m":"auditCourierNumber"};
	var show=$("#auditPromoptWin .show");
	show.html("快递单正在审核中。。。");
	common.showProgress();
	var rjson=common.ajaxRequest( 'service' , datas, "POST");
	common.hideProgress();
	if(rjson.result=="0")
	{
		datas={"p":"redPackActiveManage","m":"queryAuditCourierNumber"};
		var qjson=common.ajaxRequest( 'service' , datas, "POST");
		if(qjson.result=="0")
		{
			var size = qjson.size;
			if(size==0){
				show.html("快递单自动审核操作完成，请进行下一步");
			}else{
				show.html("快递单还剩有："+size+"个.</br>(500单一循环)");
			}
		}
	}
}

/**
 * 筛选刷单快递员
 * @returns
 */
var clearCourier = function(){
	var date = $('#freshCourierWin .sendTime').val();
	if(date==""||date==null){
		return alert("请填写日期");
	}
	var datas={"p":"redPackActiveManage","m":"clearCourier","clearDate":date};
	var rjson=common.ajaxRequest('service', datas, "post");
	if(rjson.result=="0"){
		$('#freshCourierWin').modal('hide');
		common.showMsg('info', '' , rjson.message);
	}else{
		common.showMsg('error', '' , '操作失败!');
	}
};

/**
 * 添加备注
 * @returns
 */
var addRemark = function(){
	var remark = $('#remarkEditWin .remark').val();
	var ids="";
	var checks=$('input[name="select"]:checked');
	var uIdsl=checks.length;
	if(uIdsl==0)
	{
		common.showMsg('error', '' , '请选择至少一条数据！');
		return;
	}
	else
	{
		for(var i=0;i<uIdsl;i++)
		{
			ids+=checks.eq(i).attr("value")+",";
		}
	}
	
	var datas={"p":"redPackActiveManage","m":"addRemark","userID":ids,"remark":remark};
	var rjson = common.ajaxRequest('service', datas , 'POST');
	if(rjson && rjson.result == 0)
	{
		activeList();
		$('#remarkEditWin').modal('hide');
		common.showMsg('info', '' , '操作成功！');
	}
	else
	{
		common.showMsg('error', '' , '操作失败！');
	}
};

/**
 * 设置/取消/名单设置
 * @returns
 */
var statusReset = function(){
	
	var type = $('#statusResetWin .type').val();
	var ids="";
	var checks=$('input[name="select"]:checked');
	var uIdsl=checks.length;
	if(uIdsl==0)
	{
		common.showMsg('error', '' , '请选择至少一条数据！');
		return;
	}
	else
	{
		for(var i=0;i<uIdsl;i++)
		{
			ids+=checks.eq(i).attr("value")+",";
		}
	}
	var flag = $('#statusResetWin .flag').val();
	var status = $('#statusResetWin .status').val();
	var datas = {};
	
	if( type == 1 ) //设置名单类型
	{
		datas={"p":"redPackActiveManage","m":"statusReset","type":type,"status":status,"ids":ids.substring(0,ids.length-1)};
	}
	else
	{
		datas={"p":"redPackActiveManage","m":"statusReset","type":type,"status":flag,"ids":ids.substring(0,ids.length-1)};
	}
	var rjson=common.ajaxRequest('service', datas, "POST");
	if( rjson && rjson.result == 0 )
	{
		activeList();
		$('#statusResetWin').modal('hide');
		common.showMsg('info', '' , '操作成功！');
	}
	else
	{
		common.showMsg('error', '' , '操作失败！');
		return;
	}
};

var showItem = function(){
	var type = $('#statusResetWin .type').val();
	if( type == 1 )
	{
		$('#statusResetWin .status').parent().parent().show();
		$('#statusResetWin .flag').parent().parent().hide();
	}
	else
	{
		$('#statusResetWin .flag').parent().parent().show();
		$('#statusResetWin .status').parent().parent().hide();
	}	
};

/**
 * 查询用户轨迹
 * @returns
 */
var showUserOpLog = function(){
	
	var nickName = $('#queryUserLogWin .nickName').val();
	var userOpTime = $('#queryUserLogWin .operDate').val();
	var userOpType = $('#queryUserLogWin .userOpType').val();
	window.location.href='page?p=page/userOperateLog&entrance=0&nickName='+nickName+
	                                    '&userOpTime='+userOpTime+'&userOpType='+userOpType;
};

/**
 * 二维码支付用户
 * @returns
 */
var qrCodePay = function(){
	var ids="";
	var checks=$('input[name="select"]:checked');
	var uIdsl=checks.length;
	if(uIdsl==0)
	{
		common.showMsg('error', '' , '请选择至少一条数据！');
		return;
	}
	else
	{
		for(var i=0;i<uIdsl;i++)
		{
			ids+=checks.eq(i).attr("value")+",";
		}
	}
	
	var datas={"p":"redPackActiveManage","m":"qrCodePay","ids":ids};
	var rjson = common.ajaxRequest('service', datas , 'POST');
	if(rjson && rjson.result == 0)
	{
		var reissueQuery=$("#reissueQuery").val();
		if(reissueQuery!=1)
		{
			$("#reissueQuery").val(1);
		}
		activeList();
		$('#setupWin').modal('hide');
		common.showMsg('info', '' , '操作成功！');
	}
	else
	{
		common.showMsg('error', '' , '操作失败！');
	}	
};

var getTheSureTime=function(emp){
	return emp>9?emp:"0"+emp;
};


